package org.cll.lagou.study.springbootdemo.controller;

import com.github.pagehelper.PageInfo;
import org.cll.lagou.study.springbootdemo.entity.Article;
import org.cll.lagou.study.springbootdemo.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/6/27 9:12
 */
@Controller
@RequestMapping(value = "/article")
public class ArticleController {

    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @ResponseBody
    @GetMapping(value = "/getById")
    public Article getById(Integer id){
        logger.info("======= getById. id = {}", id);
        Article article = articleService.getById(id);
        return article;
    }

    @ResponseBody
    @GetMapping(value = "/getAll")
    public List<Article> getAll(){
        logger.info("======= getAll");
        return articleService.getAll();
    }

    @RequestMapping(value = "/view")
    public String view(Model model){
        List<Article> articleList = articleService.getAll();
        model.addAttribute("articleList", articleList);
        return "client/index";
    }

    @RequestMapping(value = "/page")
    public String page(Model model, @RequestParam(name = "pageNum", required = false, defaultValue = "0") Integer pageNum, @RequestParam(name = "pageSize", required = false, defaultValue = "2") Integer pageSize){
        PageInfo<Article> pageInfo = articleService.getByPage(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "client/index";
    }
}